

package com.maoshi.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPayService;
import com.maoshi.shop.bean.enums.AuditStatus;
import com.maoshi.shop.bean.enums.EnterpriseApplyType;
import com.maoshi.shop.bean.enums.ShopWalletChangeReason;
import com.maoshi.shop.bean.event.EnterprisePayFailEvent;
import com.maoshi.shop.bean.event.EnterprisePaySuccessEvent;
import com.maoshi.shop.bean.model.EnterprisePay;
import com.maoshi.shop.bean.model.ShopWallet;
import com.maoshi.shop.bean.model.ShopWithdrawCash;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.Arith;
import com.maoshi.shop.dao.EnterprisePayMapper;
import com.maoshi.shop.dao.ShopWithdrawCashMapper;
import com.maoshi.shop.mp.comment.config.bean.WxMiniApp;
import com.maoshi.shop.mp.comment.config.bean.WxPay;
import com.maoshi.shop.service.EnterprisePayService;
import com.maoshi.shop.service.ShopWalletLogService;
import com.maoshi.shop.service.ShopWalletService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author
 * @date 2019-10-15 16:44:50
 */
@Service
@AllArgsConstructor
public class EnterprisePayServiceImpl extends ServiceImpl<EnterprisePayMapper, EnterprisePay> implements EnterprisePayService {

    private final EnterprisePayMapper enterprisePayMapper;

    private final EntPayService entPayService;

    private final WxPay wxPay;

    private final WxMiniApp wxMiniApp;

    private final ShopWalletLogService shopWalletLogService;

    private final ShopWithdrawCashMapper shopWithdrawCashMapper;

    private final MapperFacade mapperFacade;

    private final ShopWalletService shopWalletService;

    private final ApplicationContext applicationContext;

    private static final String CLIENT_IP = "127.0.0.1";


    @Override
    public List<EnterprisePay> listApplyEnterprisePay() {
        return enterprisePayMapper.listApplyEnterprisePay();
    }

    @Override
    public void sendEnterprisePay(List<EnterprisePay> enterprisePayList) {
        if (CollectionUtils.isEmpty(enterprisePayList)) {
            return;
        }
        for (EnterprisePay enterprisePay : enterprisePayList) {
            try {
                EntPayRequest entPayRequest = new EntPayRequest();
                entPayRequest.setMchAppid(wxMiniApp.getAppid());
                entPayRequest.setMchId(wxPay.getMchId());
                entPayRequest.setPartnerTradeNo(String.valueOf(enterprisePay.getEntPayOrderNo()));
                entPayRequest.setOpenid(enterprisePay.getOpenId());
                entPayRequest.setAmount((int) Arith.mul(enterprisePay.getAmount(), 100.0));
                entPayRequest.setCheckName("NO_CHECK");
                if (Objects.equals(enterprisePay.getType(), EnterpriseApplyType.SHOP_WITHDRAW.value())) {
                    entPayRequest.setDescription("商家提现发放");
                } else {
                    entPayRequest.setDescription("提现发放");
                }
                entPayRequest.setSpbillCreateIp(CLIENT_IP);
                entPayService.entPay(entPayRequest);
            } catch (WxPayException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySuccess(EnterprisePay enterprisePay) {
        //更新企业支付信息
        if (enterprisePayMapper.paySuccess(enterprisePay.getEntPayOrderNo(), enterprisePay.getVersion()) != 1) {
            throw new MaoshiShopBindException("更新余额记录失败，请稍后再试");
        }
        // 推送支付成功事件
        applicationContext.publishEvent(new EnterprisePaySuccessEvent(enterprisePay));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payFailed(EnterprisePay enterprisePay) {
        //更新企业支付信息
        if (enterprisePayMapper.payFailed(enterprisePay.getEntPayOrderNo(), enterprisePay.getVersion()) != 1) {
            throw new MaoshiShopBindException("更新余额记录失败，请稍后再试");
        }
        // 推送支付失败事件
        applicationContext.publishEvent(new EnterprisePayFailEvent(enterprisePay));

    }
}
